~ chicken-core (master) /manual/Module (chicken locative)
Trap1[[tags: manual]]2[[toc:]]34== Module (chicken locative)56A ''locative'' is an object that points to an element of a containing object,7much like a ''pointer'' in low-level, imperative programming languages like ''C''. The element can8be accessed and changed indirectly, by performing access or change operations9on the locative. The container object can be computed by calling the10{{locative->object}} procedure.1112Locatives may be passed to foreign procedures that expect pointer arguments.1314The following procedures are provided by the {{(chicken locative)}}15module.1617=== make-locative1819<procedure>(make-locative OBJ [INDEX])</procedure>2021Creates a locative that refers to the element of the non-immediate object22{{OBJ}} at position {{INDEX}}. {{OBJ}} may be a vector, pair, string, bytevector,23SRFI-4 number-vector, or record structure. {{INDEX}} should be a fixnum.24{{INDEX}} defaults to 0. Note that {{INDEX}} designates a character-position25for string objects, not a byte offset.262728=== make-weak-locative2930<procedure>(make-weak-locative OBJ [INDEX])</procedure>3132Creates a ''weak'' locative. Even though the locative refers to an element of a container object,33the container object will still be reclaimed by garbage collection if no other references34to it exist.353637=== locative?3839<procedure>(locative? X)</procedure>4041Returns {{#t}} if {{X}} is a locative, or {{#f}} otherwise.424344=== locative-ref4546<procedure>(locative-ref LOC)</procedure>4748Returns the element to which the locative {{LOC}} refers. If the containing49object has been reclaimed by garbage collection, an error is signalled.5051 (locative-ref (make-locative "abc" 1)) ==> #\b5253=== locative-set!5455<procedure>(locative-set! LOC X)</procedure><br>56<procedure>(set! (locative-ref LOC) X)</procedure>5758Changes the element to which the locative {{LOC}} refers to {{X}}.59If the containing60object has been reclaimed by garbage collection, an error is signalled.616263=== locative->object6465<procedure>(locative->object LOC)</procedure>6667Returns the object that contains the element referred to by {{LOC}} or68{{#f}} if the container has been reclaimed by garbage collection.6970 (locative->object (make-locative "abc" 1)) ==> "abc"7172---73Previous: [[Module (chicken load)]]7475Next: [[Module (chicken memory)]]